/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package de.tor.tribes.reportserver.ui;

import com.vaadin.terminal.Resource;
import com.vaadin.terminal.ThemeResource;
import com.vaadin.ui.CustomComponent;
import com.vaadin.ui.GridLayout;
import com.vaadin.ui.Label;
import com.vaadin.ui.Table;
import de.tor.tribes.reportserver.MyVaadinApplication;
import de.tor.tribes.reportserver.PersistenceImpl;
import de.tor.tribes.reportserver.util.ReportFormatter;
import de.tor.tribes.reportserver.util.UIUtil;
import de.tor.tribes.types.DSUnit;
import de.tor.tribes.types.ZebraReport;
import de.tor.tribes.types.ZebraReportBuilding;
import de.tor.tribes.types.ZebraReportAccessGrant;
import de.tor.tribes.types.ZebraReportUnit;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Hashtable;
import java.util.LinkedList;
import java.util.List;
import java.util.Map.Entry;
import java.util.Set;

/**
 *
 * @author Torridity
 */
public class ReportRenderPanel extends CustomComponent {

    private GridLayout mainLayout;
    private Label reportColorIconLabel;
    private Label winnerStringLabel;
    private Label sendDate;
    private Label moralLabel;
    private Label luckBarLabel;
    private Label attackerLabel;
    private Label sourceLabel;
    private Table attackerUnitTable;
    private Label defenderLabel;
    private Label targetLabel;
    private Table defenderUnitTable;
    private Label haulLabel;
    private Label spiedResourcesLabel;
    private Table buildingsTable;
    private Label acceptanceLabel;
    private boolean INITIALIZED = false;
    private PersistenceImpl persistence;

    public ReportRenderPanel(PersistenceImpl pPersistence) {
        persistence = pPersistence;
        buildMainLayout();
        setCompositionRoot(mainLayout);
    }

    private void buildMainLayout() {
        reportColorIconLabel = new Label("........");
        reportColorIconLabel.setStyleName("green");
        winnerStringLabel = new Label();
        winnerStringLabel.setWidth("100%");
        sendDate = new Label();
        sendDate.setWidth("100%");
        sendDate.setValue("");
        moralLabel = new Label();
        moralLabel.setWidth("100%");
        luckBarLabel = new Label();
        luckBarLabel.setWidth("100%");
        luckBarLabel.setContentMode(Label.CONTENT_XHTML);
        attackerLabel = new Label();
        sourceLabel = new Label();
        attackerUnitTable = new Table();
        attackerUnitTable.setWidth("100%");
        attackerUnitTable.setPageLength(5);
        defenderLabel = new Label();
        targetLabel = new Label();
        defenderUnitTable = new Table();
        defenderUnitTable.setWidth("100%");
        defenderUnitTable.setPageLength(5);

        haulLabel = new Label("Keine Informationen", Label.CONTENT_XHTML);
        haulLabel.setEnabled(false);
        spiedResourcesLabel = new Label("Keine Informationen", Label.CONTENT_XHTML);
        spiedResourcesLabel.setEnabled(false);

        buildingsTable = new Table();
        buildingsTable.setWidth("100%");

        //setup buildings table
        buildingsTable.addContainerProperty("buildingIcon", Resource.class, null);
        buildingsTable.addContainerProperty("name", String.class, null);
        buildingsTable.addContainerProperty("level", String.class, "?");
        buildingsTable.addContainerProperty("damageType", Label.class, null);
        buildingsTable.addContainerProperty("damageChange", String.class, "-");

        buildingsTable.setColumnWidth("buildingIcon", 30);
        buildingsTable.setColumnWidth("level", 30);
        buildingsTable.setColumnWidth("damageType", 30);

        buildingsTable.setSizeFull();

        buildingsTable.setColumnHeaders(new String[]{"Icon", "Gebäude", "Stufe", "", "Beschädigung"});
        buildingsTable.setColumnCollapsingAllowed(true);
        // buildingsTable.setColumnCollapsible("buildingIcon", true);
        buildingsTable.setColumnCollapsed("buildingIcon", true);
        buildingsTable.setItemIconPropertyId("buildingIcon");
        buildingsTable.setRowHeaderMode(Table.ROW_HEADER_MODE_ICON_ONLY);
        acceptanceLabel = new Label();
        acceptanceLabel.setWidth("100%");
        /*mainLayout = new UIUtil.GridLayoutBuilder(4, 16).addComponent(reportColorIconLabel, 0, 0, 1, 1).addComponent(winnerStringLabel, 1, 0, 3, 1).
         addComponent(new Label("<b>Gesendet:</b>", Label.CONTENT_XHTML), 0, 1, 1, 1).addComponent(sendDate, 1, 1, 3, 1).
         addComponent(new Label("<b>Moral:</b>", Label.CONTENT_XHTML), 0, 2, 1, 1).addComponent(moralLabel, 1, 2, 3, 1).
         addComponent(new Label("<b>Glück:</b>", Label.CONTENT_XHTML), 0, 3, 1, 1).addComponent(luckBarLabel, 1, 3, 3, 1).
         addComponent(new Label("<b>Angreifer:</b>", Label.CONTENT_XHTML), 0, 4, 1, 1).addComponent(attackerLabel, 1, 4, 3, 1).
         addComponent(new Label("<b>Herkunft:</b>", Label.CONTENT_XHTML), 0, 5, 1, 1).addComponent(sourceLabel, 1, 5, 3, 1).
         addComponent(attackerUnitTable, 0, 6, 4, 1).
         addComponent(new Label("<b>Verteidiger:</b>", Label.CONTENT_XHTML), 0, 7, 1, 1).addComponent(defenderLabel, 1, 7, 3, 1).
         addComponent(new Label("<b>Ziel:</b>", Label.CONTENT_XHTML), 0, 8, 1, 1).addComponent(targetLabel, 1, 8, 3, 1).
         addComponent(defenderUnitTable, 0, 9, 4, 2).
         addComponent(new Label("<b>Zustimmung:</b>", Label.CONTENT_XHTML), 0, 11, 1, 1).addComponent(acceptanceLabel, 1, 11, 3, 1).
         addComponent(new Label("<b>Beute:</b>", Label.CONTENT_XHTML), 0, 12, 1, 1).addComponent(haulLabel, 1, 12, 3, 1).
         addComponent(new Label("<b>Erspähte Rohstoffe:</b>", Label.CONTENT_XHTML), 0, 13, 1, 1).addComponent(spiedResourcesLabel, 1, 13, 3, 1).
         addComponent(buildingsTable, 0, 14, 4, 2).getLayout();
                
         mainLayout.setColumnExpandRatio(0, .2f);
         mainLayout.setColumnExpandRatio(1, 0.8f);
         mainLayout.setRowExpandRatio(14, 1.0f);
         */
        mainLayout = new UIUtil.GridLayoutBuilder(4, 13).addComponent(reportColorIconLabel, 0, 0, 1, 1).addComponent(winnerStringLabel, 1, 0, 3, 1).
                addComponent(new Label("<b>Gesendet:</b>", Label.CONTENT_XHTML), 0, 1, 1, 1).addComponent(sendDate, 1, 1, 3, 1).
                addComponent(new Label("<b>Moral:</b>", Label.CONTENT_XHTML), 0, 2, 1, 1).addComponent(moralLabel, 1, 2, 3, 1).
                addComponent(new Label("<b>Glück:</b>", Label.CONTENT_XHTML), 0, 3, 1, 1).addComponent(luckBarLabel, 1, 3, 3, 1).
                addComponent(new Label("<b>Angreifer:</b>", Label.CONTENT_XHTML), 0, 4, 1, 1).addComponent(attackerLabel, 1, 4, 1, 1).addComponent(new Label("<b>Verteidiger:</b>", Label.CONTENT_XHTML), 2, 4, 1, 1).addComponent(defenderLabel, 3, 4, 1, 1).
                addComponent(new Label("<b>Herkunft:</b>", Label.CONTENT_XHTML), 0, 5, 1, 1).addComponent(sourceLabel, 1, 5, 1, 1).addComponent(new Label("<b>Ziel:</b>", Label.CONTENT_XHTML), 2, 5, 1, 1).addComponent(targetLabel, 3, 5, 1, 1).
                addComponent(attackerUnitTable, 0, 6, 2, 2).addComponent(defenderUnitTable, 2, 6, 2, 2).
                addComponent(new Label("<b>Zustimmung:</b>", Label.CONTENT_XHTML), 0, 8, 1, 1).addComponent(acceptanceLabel, 1, 8, 3, 1).
                addComponent(new Label("<b>Beute:</b>", Label.CONTENT_XHTML), 0, 9, 1, 1).addComponent(haulLabel, 1, 9, 3, 1).
                addComponent(new Label("<b>Erspähte Rohstoffe:</b>", Label.CONTENT_XHTML), 0, 10, 1, 1).addComponent(spiedResourcesLabel, 1, 10, 3, 1).
                addComponent(buildingsTable, 0, 11, 4, 2).getLayout();

        mainLayout.setColumnExpandRatio(0, .1f);
        mainLayout.setColumnExpandRatio(1, 0.4f);
        mainLayout.setColumnExpandRatio(2, .1f);
        mainLayout.setColumnExpandRatio(3, 0.4f);

        mainLayout.setRowExpandRatio(11, 1.0f);

        mainLayout.setMargin(true);
        mainLayout.setSpacing(true);
        mainLayout.setSizeFull();



        mainLayout.setStyleName("zebrareport");
    }

    /*  public void render(ZebraReport pReport) {
     if (!INITIALIZED) {
     //setup troops tables
     List<ThemeResource> icons = new LinkedList<ThemeResource>();
     List<String> names = new LinkedList<String>();
     List<String> ids = new LinkedList<String>();
     names.add("");
     ids.add("type");
     icons.add(null);
     attackerUnitTable.addContainerProperty("type", String.class, null);
     defenderUnitTable.addContainerProperty("type", String.class, null);
     ThemeResource militia = null;
     for (DSUnit unit : ((MyVaadinApplication) getApplication()).getPersistence().getUnits()) {
     ThemeResource res = new ThemeResource("../zebra/img/units/" + unit.getPlainName() + ".png");
     if (unit.getPlainName().equals("militia")) {
     militia = res;
     defenderUnitTable.addContainerProperty(unit.getPlainName(), String.class, 0);
     } else {
     attackerUnitTable.addContainerProperty(unit.getPlainName(), String.class, 0);
     defenderUnitTable.addContainerProperty(unit.getPlainName(), String.class, 0);
     }
     icons.add(res);
     ids.add(unit.getPlainName());
     names.add("");
     }
     defenderUnitTable.setVisibleColumns(ids.toArray(new String[]{}));
     defenderUnitTable.setColumnHeaders(names.toArray(new String[]{}));
     defenderUnitTable.setColumnIcons(icons.toArray(new ThemeResource[]{}));
     defenderUnitTable.setColumnCollapsingAllowed(true);

     icons.remove(militia);
     ids.remove("militia");
     names.remove(1);
     attackerUnitTable.setVisibleColumns(ids.toArray(new String[]{}));
     attackerUnitTable.setColumnHeaders(names.toArray(new String[]{}));
     attackerUnitTable.setColumnIcons(icons.toArray(new ThemeResource[]{}));
     attackerUnitTable.setColumnCollapsingAllowed(true);
     INITIALIZED = true;
     }


     String resourcePath = getApplication().getURL().toString();
     switch (pReport.getColor()) {
     case BLUE:
     reportColorIconLabel.setStyleName("blue");
     break;
     case GREEN:
     reportColorIconLabel.setStyleName("green");
     break;
     case YELLOW:
     reportColorIconLabel.setStyleName("yellow");
     break;
     case RED:
     reportColorIconLabel.setStyleName("red");
     break;
     case RED_BLUE:
     reportColorIconLabel.setStyleName("redblue");
     break;
     case RED_YELLOW:
     reportColorIconLabel.setStyleName("redyellow");
     break;
     default:
     reportColorIconLabel.setStyleName("unknown");

     }

     winnerStringLabel.setValue((pReport.isWon()) ? "Der Angreifer hat gewonnen" : "Der Verteidiger hat gewonnen");
     sendDate.setValue(new SimpleDateFormat("dd.MM.yy HH:mm:ss").format(new Date(pReport.getSent())));

     moralLabel.setValue(pReport.getMoral() + "%");

     luckBarLabel.setValue(ReportFormatter.buildLuckBar(resourcePath, pReport.getLuck()));

     attackerLabel.setValue(pReport.getAttacker());
     sourceLabel.setValue(pReport.getSource());

     attackerUnitTable.removeAllItems();
     defenderUnitTable.removeAllItems();
     Object attackerAmountId = attackerUnitTable.addItem();
     attackerUnitTable.getContainerProperty(attackerAmountId, "type").setValue("Anzahl:");
     Object lostAttackerId = attackerUnitTable.addItem();
     attackerUnitTable.getContainerProperty(lostAttackerId, "type").setValue("Verluste:");
     Object defenderAmountId = defenderUnitTable.addItem();
     defenderUnitTable.getContainerProperty(defenderAmountId, "type").setValue("Anzahl:");
     Object lostDefenderId = defenderUnitTable.addItem();
     defenderUnitTable.getContainerProperty(lostDefenderId, "type").setValue("Verluste:");
     Object onTheWayId = defenderUnitTable.addItem();
     defenderUnitTable.getContainerProperty(onTheWayId, "type").setValue("Unterwegs:");
     Object outsideId = defenderUnitTable.addItem();
     defenderUnitTable.getContainerProperty(outsideId, "type").setValue("Außerhalb:");

     Hashtable<String, Integer> outsideUnits = new Hashtable<String, Integer>();

     Hashtable<DSUnit, Boolean> visibility = new Hashtable<DSUnit, Boolean>();
     for (DSUnit u : ((MyVaadinApplication) getApplication()).getPersistence().getUnits()) {
     visibility.put(u, Boolean.FALSE);
     }

     for (ZebraReportUnit u : pReport.getUnits()) {
     DSUnit unit = u.getUnit();
     visibility.put(unit, Boolean.TRUE);
     switch (u.getType()) {
     case ATTACKER: {
     attackerUnitTable.getContainerProperty(attackerAmountId, unit.getPlainName()).setValue(Integer.toString(u.getAmount()));
     break;
     }
     case LOST_ATTACKERS: {
     attackerUnitTable.getContainerProperty(lostAttackerId, unit.getPlainName()).setValue(Integer.toString(u.getAmount()));
     break;
     }
     case DEFENDER: {
     defenderUnitTable.getContainerProperty(defenderAmountId, unit.getPlainName()).setValue(Integer.toString(u.getAmount()));
     break;
     }
     case LOST_DEFENDERS: {
     defenderUnitTable.getContainerProperty(lostDefenderId, unit.getPlainName()).setValue(Integer.toString(u.getAmount()));
     break;
     }
     case ON_THE_WAY: {
     defenderUnitTable.getContainerProperty(onTheWayId, unit.getPlainName()).setValue(Integer.toString(u.getAmount()));
     break;
     }
     case OUTSIDE: {
     Integer amount = outsideUnits.get(unit.getPlainName());
     if (amount == null) {
     outsideUnits.put(unit.getPlainName(), u.getAmount());
     } else {
     outsideUnits.put(unit.getPlainName(), amount + u.getAmount());
     }
     }
     }
     }

     Set<Entry<String, Integer>> entries = outsideUnits.entrySet();
     for (Entry<String, Integer> e : entries) {
     defenderUnitTable.getContainerProperty(onTheWayId, e.getKey()).setValue(Integer.toString(e.getValue()));
     }
     Set<Entry<DSUnit, Boolean>> visEntries = visibility.entrySet();
     for (Entry<DSUnit, Boolean> e : visEntries) {
     attackerUnitTable.setColumnCollapsed(e.getKey().getPlainName(), !e.getValue());
     defenderUnitTable.setColumnCollapsed(e.getKey().getPlainName(), !e.getValue());
     }

     defenderLabel.setValue(pReport.getDefender());
     targetLabel.setValue(pReport.getTarget());
     String woodIcon = resourcePath + "VAADIN/themes/zebra/img/wood.png";
     String clayIcon = resourcePath + "VAADIN/themes/zebra/img/clay.png";
     String ironIcon = resourcePath + "VAADIN/themes/zebra/img/iron.png";

     if (pReport.getWoodHaul() >= 0) {
     haulLabel.setValue(pReport.getWoodHaul()
     + " <img src=\"" + woodIcon + "\"/> "
     + pReport.getClayHaul()
     + " <img src=\"" + clayIcon + "\"/> "
     + pReport.getIronHaul()
     + " <img src=\"" + ironIcon + "\"/>");
     haulLabel.setEnabled(true);
     } else {
     haulLabel.setValue("Keine Beute");
     haulLabel.setEnabled(false);
     }
     if (pReport.getSpiedWood() >= 0) {
     spiedResourcesLabel.setValue(pReport.getSpiedWood()
     + " <img src=\"" + woodIcon + "\"/> "
     + pReport.getSpiedClay()
     + " <img src=\"" + clayIcon + "\"/> "
     + pReport.getSpiedIron()
     + " <img src=\"" + ironIcon + "\"/>");
     spiedResourcesLabel.setEnabled(true);
     } else {
     spiedResourcesLabel.setValue("Keine Informationen");
     spiedResourcesLabel.setEnabled(false);
     }

     if (pReport.getAcceptBefore() != pReport.getAcceptAfter()) {
     acceptanceLabel.setValue("Gesenkt von " + pReport.getAcceptBefore() + " auf " + pReport.getAcceptAfter());
     acceptanceLabel.setEnabled(true);
     } else {
     acceptanceLabel.setValue("Keine Veränderung");
     acceptanceLabel.setEnabled(false);
     }

     buildingsTable.removeAllItems();
     for (ZebraReportBuilding building : pReport.getBuildings(true)) {
     buildingsTable.addItem(building.getBuilding().getPlainName());
     buildingsTable.getContainerProperty(building.getBuilding().getPlainName(), "buildingIcon").setValue(new ThemeResource("../zebra/img/buildings/" + building.getBuilding().getPlainName() + ".png"));
     buildingsTable.getContainerProperty(building.getBuilding().getPlainName(), "name").setValue(building.getBuilding().getName());
     buildingsTable.getContainerProperty(building.getBuilding().getPlainName(), "level").setValue(building.getLevelBefore());
     }

     if (buildingsTable.getItemIds().isEmpty()) {
     buildingsTable.setVisible(false);
     } else {
     buildingsTable.setVisible(true);
     ZebraReportBuilding damagedWall = pReport.getWallDamage();
     if (damagedWall != null) {

     Label l = new Label("<img src=\"" + resourcePath + "VAADIN/themes/zebra/img/units/ram.png\"/>", Label.CONTENT_XHTML);
     buildingsTable.getContainerProperty(damagedWall.getBuilding().getPlainName(), "damageType").setValue(l);
     buildingsTable.getContainerProperty(damagedWall.getBuilding().getPlainName(), "damageChange").setValue("Zerstört von Level " + damagedWall.getLevelBefore() + " auf " + damagedWall.getLevelAfter());
     }

     ZebraReportBuilding damagedBuilding = pReport.getBuildingDamage();
     if (damagedBuilding != null) {
     Label l = new Label("<img src=\"" + resourcePath + "VAADIN/themes/zebra/img/units/catapult.png\"/>", Label.CONTENT_XHTML);
     buildingsTable.getContainerProperty(damagedBuilding.getBuilding().getPlainName(), "damageType").setValue(l);
     buildingsTable.getContainerProperty(damagedBuilding.getBuilding().getPlainName(), "damageChange").setValue("Zerstört von Level " + damagedBuilding.getLevelBefore() + " auf " + damagedBuilding.getLevelAfter());
     }
     }
     }
     */
    public void render(ZebraReportAccessGrant pReportShare, String pResourcePath) {
        List<DSUnit> units = persistence.getUnits();
        if (!INITIALIZED) {
            //setup troops tables
            List<ThemeResource> icons = new LinkedList<ThemeResource>();
            List<String> names = new LinkedList<String>();
            List<String> ids = new LinkedList<String>();
            names.add("");
            ids.add("type");
            icons.add(null);
            attackerUnitTable.addContainerProperty("type", String.class, null);
            defenderUnitTable.addContainerProperty("type", String.class, null);
            ThemeResource militia = null;
            for (DSUnit unit : units) {
                ThemeResource res = new ThemeResource("../zebra/img/units/" + unit.getPlainName() + ".png");
                if (unit.getPlainName().equals("militia")) {
                    militia = res;
                    defenderUnitTable.addContainerProperty(unit.getPlainName(), String.class, 0);
                } else {
                    attackerUnitTable.addContainerProperty(unit.getPlainName(), String.class, 0);
                    defenderUnitTable.addContainerProperty(unit.getPlainName(), String.class, 0);
                }
                icons.add(res);
                ids.add(unit.getPlainName());
                names.add("");
            }
            defenderUnitTable.setVisibleColumns(ids.toArray(new String[]{}));
            defenderUnitTable.setColumnHeaders(names.toArray(new String[]{}));
            defenderUnitTable.setColumnIcons(icons.toArray(new ThemeResource[]{}));
            defenderUnitTable.setColumnCollapsingAllowed(true);

            icons.remove(militia);
            ids.remove("militia");
            names.remove(1);
            attackerUnitTable.setVisibleColumns(ids.toArray(new String[]{}));
            attackerUnitTable.setColumnHeaders(names.toArray(new String[]{}));
            attackerUnitTable.setColumnIcons(icons.toArray(new ThemeResource[]{}));
            attackerUnitTable.setColumnCollapsingAllowed(true);
            INITIALIZED = true;
        }

        ZebraReport report = pReportShare.getReport();
        switch (report.getColor()) {
            case BLUE:
                reportColorIconLabel.setStyleName("blue");
                break;
            case GREEN:
                reportColorIconLabel.setStyleName("green");
                break;
            case YELLOW:
                reportColorIconLabel.setStyleName("yellow");
                break;
            case RED:
                reportColorIconLabel.setStyleName("red");
                break;
            case RED_BLUE:
                reportColorIconLabel.setStyleName("redblue");
                break;
            case RED_YELLOW:
                reportColorIconLabel.setStyleName("redyellow");
                break;
            default:
                reportColorIconLabel.setStyleName("unknown");

        }

        winnerStringLabel.setValue((report.isWon()) ? "Der Angreifer hat gewonnen" : "Der Verteidiger hat gewonnen");
        sendDate.setValue(new SimpleDateFormat("dd.MM.yy HH:mm:ss").format(new Date(report.getSent())));

        moralLabel.setValue(report.getMoral() + "%");

        luckBarLabel.setValue(ReportFormatter.buildLuckBar(pResourcePath, report.getLuck()));
        attackerLabel.setValue(report.getAttacker());
        if (pReportShare.isShareSource()) {
            sourceLabel.setValue(report.getSource());
        } else {
            sourceLabel.setValue("Vom Besitzer verborgen");
        }

        attackerUnitTable.removeAllItems();
        defenderUnitTable.removeAllItems();
        Object attackerAmountId = attackerUnitTable.addItem();
        attackerUnitTable.getContainerProperty(attackerAmountId, "type").setValue("Anzahl:");
        Object lostAttackerId = attackerUnitTable.addItem();
        attackerUnitTable.getContainerProperty(lostAttackerId, "type").setValue("Verluste:");
        Object defenderAmountId = defenderUnitTable.addItem();
        defenderUnitTable.getContainerProperty(defenderAmountId, "type").setValue("Anzahl:");
        Object lostDefenderId = defenderUnitTable.addItem();
        defenderUnitTable.getContainerProperty(lostDefenderId, "type").setValue("Verluste:");
        Object onTheWayId = defenderUnitTable.addItem();
        defenderUnitTable.getContainerProperty(onTheWayId, "type").setValue("Unterwegs:");
        Object outsideId = defenderUnitTable.addItem();
        defenderUnitTable.getContainerProperty(outsideId, "type").setValue("Außerhalb:");

        Hashtable<String, Integer> outsideUnits = new Hashtable<String, Integer>();

        Hashtable<DSUnit, Boolean> visibility = new Hashtable<DSUnit, Boolean>();
        for (DSUnit u : units) {
            visibility.put(u, Boolean.FALSE);
        }

        for (ZebraReportUnit u : report.getUnits()) {
            DSUnit unit = u.getUnit();
            visibility.put(unit, Boolean.TRUE);
            switch (u.getType()) {
                case ATTACKER: {
                    if (pReportShare.isShareAttackers()) {
                        attackerUnitTable.getContainerProperty(attackerAmountId, unit.getPlainName()).setValue(Integer.toString(u.getAmount()));
                    } else {
                        attackerUnitTable.getContainerProperty(attackerAmountId, unit.getPlainName()).setValue("-");
                    }
                    break;
                }
                case LOST_ATTACKERS: {
                    if (pReportShare.isShareAttackerLosses()) {
                        attackerUnitTable.getContainerProperty(lostAttackerId, unit.getPlainName()).setValue(Integer.toString(u.getAmount()));
                    } else {
                        attackerUnitTable.getContainerProperty(lostAttackerId, unit.getPlainName()).setValue("-");
                    }
                    break;
                }
                case DEFENDER: {
                    if (pReportShare.isShareDefenders()) {
                        defenderUnitTable.getContainerProperty(defenderAmountId, unit.getPlainName()).setValue(Integer.toString(u.getAmount()));
                    } else {
                        defenderUnitTable.getContainerProperty(defenderAmountId, unit.getPlainName()).setValue("-");
                    }
                    break;
                }
                case LOST_DEFENDERS: {
                    if (pReportShare.isShareDefenderLosses()) {
                        defenderUnitTable.getContainerProperty(lostDefenderId, unit.getPlainName()).setValue(Integer.toString(u.getAmount()));
                    } else {
                        defenderUnitTable.getContainerProperty(lostDefenderId, unit.getPlainName()).setValue("-");
                    }
                    break;
                }
                case ON_THE_WAY: {
                    if (pReportShare.isShareSpyInfo()) {
                        defenderUnitTable.getContainerProperty(onTheWayId, unit.getPlainName()).setValue(Integer.toString(u.getAmount()));
                    } else {
                        defenderUnitTable.getContainerProperty(onTheWayId, unit.getPlainName()).setValue("-");
                    }
                    break;
                }
                case OUTSIDE: {
                    Integer amount = outsideUnits.get(unit.getPlainName());
                    if (amount == null) {
                        outsideUnits.put(unit.getPlainName(), u.getAmount());
                    } else {
                        outsideUnits.put(unit.getPlainName(), amount + u.getAmount());
                    }
                }
            }
        }

        Set<Entry<String, Integer>> entries = outsideUnits.entrySet();
        for (Entry<String, Integer> e : entries) {
            if (pReportShare.isShareSpyInfo()) {
                defenderUnitTable.getContainerProperty(onTheWayId, e.getKey()).setValue(Integer.toString(e.getValue()));
            } else {
                defenderUnitTable.getContainerProperty(onTheWayId, e.getKey()).setValue("-");
            }
        }
        Set<Entry<DSUnit, Boolean>> visEntries = visibility.entrySet();
        for (Entry<DSUnit, Boolean> e : visEntries) {
            attackerUnitTable.setColumnCollapsed(e.getKey().getPlainName(), !e.getValue());
            defenderUnitTable.setColumnCollapsed(e.getKey().getPlainName(), !e.getValue());
        }

        defenderLabel.setValue(report.getDefender());
        if (pReportShare.isShareTarget()) {
            targetLabel.setValue(report.getTarget());
        } else {
            targetLabel.setValue("Vom Besitzer verborgen");
        }

        String woodIcon = pResourcePath + "VAADIN/themes/zebra/img/wood.png";
        String clayIcon = pResourcePath + "VAADIN/themes/zebra/img/clay.png";
        String ironIcon = pResourcePath + "VAADIN/themes/zebra/img/iron.png";

        if (report.getWoodHaul() >= 0 && pReportShare.isShareHaul()) {
            haulLabel.setValue(report.getWoodHaul()
                    + " <img src=\"" + woodIcon + "\"/> "
                    + report.getClayHaul()
                    + " <img src=\"" + clayIcon + "\"/> "
                    + report.getIronHaul()
                    + " <img src=\"" + ironIcon + "\"/>");
            haulLabel.setEnabled(true);
        } else {
            haulLabel.setValue("Vom Besitzer verborgen oder keine Beute vorhanden");
            haulLabel.setEnabled(false);
        }

        if (report.getSpiedWood() >= 0 && pReportShare.isShareSpyInfo()) {
            spiedResourcesLabel.setValue(report.getSpiedWood()
                    + " <img src=\"" + woodIcon + "\"/> "
                    + report.getSpiedClay()
                    + " <img src=\"" + clayIcon + "\"/> "
                    + report.getSpiedIron()
                    + " <img src=\"" + ironIcon + "\"/>");
            spiedResourcesLabel.setEnabled(true);
        } else {
            spiedResourcesLabel.setValue("Vom Besitzer verborgen oder keine Rohstoffe vorhanden");
            spiedResourcesLabel.setEnabled(false);
        }

        if (report.getAcceptBefore() != report.getAcceptAfter()) {
            acceptanceLabel.setValue("Gesenkt von " + report.getAcceptBefore() + " auf " + report.getAcceptAfter());
            acceptanceLabel.setEnabled(true);
        } else {
            acceptanceLabel.setValue("Keine Veränderung");
            acceptanceLabel.setEnabled(false);
        }

        buildingsTable.removeAllItems();
        for (ZebraReportBuilding building : report.getBuildings(true)) {
            buildingsTable.addItem(building.getBuilding().getPlainName());
            buildingsTable.getContainerProperty(building.getBuilding().getPlainName(), "buildingIcon").setValue(new ThemeResource("../zebra/img/buildings/" + building.getBuilding().getPlainName() + ".png"));
            buildingsTable.getContainerProperty(building.getBuilding().getPlainName(), "name").setValue(building.getBuilding().getName());
            if (pReportShare.isShareSpyInfo()) {
                buildingsTable.getContainerProperty(building.getBuilding().getPlainName(), "level").setValue(building.getLevelBefore());
            } else {
                buildingsTable.getContainerProperty(building.getBuilding().getPlainName(), "level").setValue("-");
            }
        }

        if (buildingsTable.getItemIds().isEmpty()) {
            buildingsTable.setVisible(false);
        } else {
            buildingsTable.setVisible(true);
            ZebraReportBuilding damagedWall = report.getWallDamage();
            if (damagedWall != null) {
                Label l = new Label("<img src=\"" + pResourcePath + "VAADIN/themes/zebra/img/units/ram.png\"/>", Label.CONTENT_XHTML);
                buildingsTable.getContainerProperty(damagedWall.getBuilding().getPlainName(), "damageType").setValue(l);
                buildingsTable.getContainerProperty(damagedWall.getBuilding().getPlainName(), "damageChange").setValue("Zerstört von Level " + damagedWall.getLevelBefore() + " auf " + damagedWall.getLevelAfter());
            }

            ZebraReportBuilding damagedBuilding = report.getBuildingDamage();
            if (damagedBuilding != null) {
                Label l = new Label("<img src=\"" + pResourcePath + "VAADIN/themes/zebra/img/units/catapult.png\"/>", Label.CONTENT_XHTML);
                buildingsTable.getContainerProperty(damagedBuilding.getBuilding().getPlainName(), "damageType").setValue(l);
                buildingsTable.getContainerProperty(damagedBuilding.getBuilding().getPlainName(), "damageChange").setValue("Zerstört von Level " + damagedBuilding.getLevelBefore() + " auf " + damagedBuilding.getLevelAfter());
            }
        }
    }
}
